/*
 * @Author: jianfanfan 1217572833@qq.com
 * @Date: 2025-03-15 17:00:39
 * @LastEditors: jianfanfan 1217572833@qq.com
 * @LastEditTime: 2025-04-16 17:27:05
 * @FilePath: \guozaoke\src\auth\auth.controller.ts
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import {
  Controller,
  Post,
  Body,
  UseGuards,
  Get,
  Headers,
  Put,
  HttpCode,
  HttpStatus,
} from '@nestjs/common';
import { RegisterDto } from './dto/register.dto';
import { LoginDto } from './dto/login.dto';
import { ForgotPasswordDto } from './dto/forgot-password.dto';
import { UserInfoDto } from './dto/user-info.dto';
import { AuthService } from './auth.service';
import {
  ApiTags,
  ApiOperation,
  ApiResponse,
  ApiBearerAuth,
} from '@nestjs/swagger';
import { ApiResponseDto } from '../common/dto/api-response.dto';

@ApiTags('Auth')
@Controller('auth')
export class AuthController {
  constructor(private readonly authService: AuthService) {}

  @Post('register')
  @HttpCode(HttpStatus.OK)
  @ApiOperation({ summary: '用户注册', description: '注册新用户' })
  @ApiResponse({
    status: HttpStatus.CREATED,
    description: '用户创建成功',
    type: ApiResponseDto,
  })
  @ApiResponse({
    status: HttpStatus.BAD_REQUEST,
    description: '请求数据验证失败',
    type: ApiResponseDto,
  })
  register(@Body() registerDto: RegisterDto) {
    return this.authService.register(registerDto);
  }

  @Post('login')
  @HttpCode(HttpStatus.OK)
  @ApiOperation({ summary: '用户登录', description: '使用用户名和密码登录' })
  @ApiResponse({
    status: HttpStatus.OK,
    description: '登录成功',
    type: ApiResponseDto,
  })
  @ApiResponse({
    status: HttpStatus.UNAUTHORIZED,
    description: '登录失败',
    type: ApiResponseDto,
  })
  login(@Body() loginDto: LoginDto) {
    return this.authService.login(loginDto);
  }

  @Post('forgot-password')
  @HttpCode(HttpStatus.OK)
  @ApiOperation({ summary: '找回密码', description: '通过邮箱找回密码' })
  @ApiResponse({
    status: HttpStatus.OK,
    description: '找回密码操作成功',
    type: ApiResponseDto,
  })
  forgotPassword(@Body() forgotPasswordDto: ForgotPasswordDto) {
    return this.authService.forgotPassword(forgotPasswordDto);
  }

  @Get('profile')
  @ApiBearerAuth('bearer')
  @ApiOperation({
    summary: '获取用户信息',
    description: '获取当前登录用户的详细信息',
  })
  @ApiResponse({
    status: HttpStatus.OK,
    description: '成功获取用户信息',
    type: ApiResponseDto,
  })
  @ApiResponse({
    status: HttpStatus.UNAUTHORIZED,
    description: '未授权',
    type: ApiResponseDto,
  })
  async getUserProfile(@Headers('authorization') auth: string) {
    const token = auth.replace('Bearer ', '');
    return this.authService.getUserInfo(token);
  }

  @Put('profile')
  @ApiBearerAuth('bearer')
  @ApiOperation({
    summary: '更新用户信息',
    description: '更新当前登录用户的个人资料',
  })
  @ApiResponse({
    status: HttpStatus.OK,
    description: '用户信息更新成功',
    type: ApiResponseDto,
  })
  @ApiResponse({
    status: HttpStatus.UNAUTHORIZED,
    description: '未授权',
    type: ApiResponseDto,
  })
  updateUserProfile(
    @Headers('authorization') auth: string,
    @Body() userInfoDto: UserInfoDto,
  ) {
    const token = auth.replace('Bearer ', '');
    return this.authService.updateUserInfo(token, userInfoDto);
  }
}
